//
//  WFDGuideView.m
//  谷居
//
//  Created by dllo on 16/7/6.
//  Copyright © 2016年 刘万方. All rights reserved.
//

#import "WFDGuideView.h"
#import "WFTableView.h"
#import "WFDGuideCell.h"
#import "MJRefresh.h"
#import "AFNetworking.h"
#import "WFDGuideModel.h"
@interface WFDGuideView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) WFTableView *tableView;

@property (nonatomic, assign) NSInteger start;

@end
@implementation WFDGuideView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame WithName:(NSString *)name {
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
        self.name = name;

        [self refresh];
        
    }
    return self;
}
- (void)refresh {
    self.start = 0;
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:self.name];
    NSString *str2 = [self.name stringByAddingPercentEncodingWithAllowedCharacters:set];
    NSString *url = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/user/%@/strategy?start=%ld&count=5&checked=1&loginId=0", str2, self.start];
    NSLog(@"%@", url);
    [self makeRequstWithUrl:url upDown:YES];
    [_tableView.mj_header endRefreshing];
}
- (void)loadMore {
    self.start += 10;
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:self.name];
    NSString *str2 = [self.name stringByAddingPercentEncodingWithAllowedCharacters:set];
    NSString *url = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/user/%@/strategy?start=%ld&count=5&checked=1&loginId=0", str2, self.start];
    [self makeRequstWithUrl:url upDown:NO];
    [_tableView.mj_footer endRefreshing];

}
- (void)makeRequstWithUrl:(NSString *)URL upDown:(BOOL)upDown {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:URL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSMutableArray *array = responseObject[@"strategyList"];
        NSLog(@"%@", array);
        if (upDown) {
            if (self.dataArray != nil) {
                [self.dataArray removeAllObjects];
            }
            self.dataArray  = [WFDGuideModel baseModelByArray:array];

        } else {
            [self.dataArray addObjectsFromArray:array];
        }
        
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}
- (void)createTableView {
    _tableView = [[WFTableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
    [self addSubview:_tableView];
    [_tableView release];
    _tableView.rowHeight = 150;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    WFDGuideCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[WFDGuideCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
//    NSLog(@"%@", self.dataArray);
//    WFDGuideModel *model = self.dataArray[indexPath.row];
//    NSLog(@"%ld", model.coverPhotoId);
    cell.model = self.dataArray[indexPath.row];
//    NSLog(@"coverId:%ld",cell.model.coverPhotoId);

    return cell;
}




#pragma mrak - 懒加载
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray array] retain];
    }
    return _dataArray;
}


@end
